package com.example.test.fragment;


import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.example.test.R;
import com.example.test.adapter.MusicAdapter;
import com.example.test.entity.Song;
import com.example.test.service.MusicService;
import com.example.test.util.MusicUtils;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class MusicFragment extends Fragment {

    private ListView music;
    private Button pause;
    private Button back;
    private Button next;
    private Button restart;

    private static MusicService.MyBinder binder;

    //接收广播发来的通知
    public static Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 101){
                binder.callpause();
            }else if (msg.what == 102){
                binder.callrestart();
            }else if (msg.what == 103){
                binder.callBack();
            }else if (msg.what == 104){
                binder.callNext();
            }
        }
    };

    public MusicFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_music, container, false);

        music = (ListView) inflate.findViewById(R.id.music);
        pause = (Button) inflate.findViewById(R.id.pause);
        back = (Button) inflate.findViewById(R.id.back);
        next = (Button) inflate.findViewById(R.id.next);
        restart = (Button) inflate.findViewById(R.id.restart);

        //暂停
        pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binder.callpause();
            }
        });

        //继续
        restart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binder.callrestart();
            }
        });

        //下一首
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binder.callNext();
            }
        });

        //上一首
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binder.callBack();
            }
        });

        //调用MusicUtils中的getMusics方法  得到歌曲集合
        List<Song> musics = MusicUtils.getMusics(getActivity());
        //添加适配器
        MusicAdapter musicAdapter = new MusicAdapter(musics,getContext());
        music.setAdapter(musicAdapter);

        //启动服务
        Intent intent = new Intent(getActivity(), MusicService.class);
        getActivity().startService(intent);
        ServiceConnection connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                binder = (MusicService.MyBinder)service;
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };
        //绑定服务
        getActivity().bindService(intent,connection, Context.BIND_AUTO_CREATE);

        music.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //Toast.makeText(getActivity(), ""+position, Toast.LENGTH_SHORT).show();
                binder.callplay(position);
            }
        });
        return inflate;


    }
}